<?php

namespace app\admin\controller\common;

use think\Controller;
use think\Request;
use think\Session;

class Base extends Controller
{
    public function _initialize()
    {
        //验证登录
        if (!Session::has('admin')) $this->redirect('/admin/login/index');
        //验证用户账号状态
        $id = Session::get('admin');
        if (db('admin')->where(['is_del' => 0, 'state' => 1, 'id' => $id])->count() == 0) $this->redirect('/admin/login/index');
        //验证权限
        $admin = db('admin')->field('superadmin,role')->where(['is_del' => 0, 'state' => 1, 'id' => $id])->find();
        if ($admin['role'] == 0 && $admin['superadmin'] == 1) {
            //超级管理员不验证权限问题
        } else {
            $request = Request::instance();
            $url = strtolower($request->url());
            $power = db('role')->where(['id' => $admin['role']])->value('power');
            $power = explode(",", $power); //权限组
            if (db('menuinfo')->where(['href' => $url])->count() == 0) { //权限租未配置
                //判断默认权限
                if (!in_array($url, config('mpower'))) $this->redirect('/admin/login/nopower');
                // $this->redirect('/admin/login/nopower');
            } else {
                $menuinfoid = db('menuinfo')->where(['href' => $url])->value('id');
                if (!in_array($menuinfoid, $power)) $this->redirect('/admin/login/nopower');
            }
        }
    }
    public function _empty()
    { //请求不存在类方法
        return $this->fetch('/error');
    }
}
